package com.own.business.comment.controller.user;

import com.own.aop.permission.PermissionModule;
import com.own.business.comment.business.service.CommentService;
import com.own.business.comment.common.constant.ConstantModule;
import com.own.business.comment.entity.dto.CommentDto;
import com.own.business.comment.entity.query.CommentQuery;
import com.own.business.comment.entity.vo.CommentVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.login.util.SessionUserUtil;
import com.own.component.common.model.PageModel;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.log.operation.aop.LogOperationAop;
import com.own.component.log.operation.aop.LogOperationModuleAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * UserCommentController
 *
 * @author chenxueli
 * @date 2023-07-03 14:15:52
 */
@Tag(name = "评论" + ConstantBeanSuffix.USER_CLIENT + ConstantBeanSuffix.CONTROLLER)
@RestController
@PermissionModule(ConstantModule.COMMENT)
@LogOperationModuleAop("评论" + ConstantBeanSuffix.USER_CLIENT)
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + ConstantApiPrefix.USER_CLIENT + "comment")
public class UserCommentController extends BaseController {

    @Resource
    private CommentService commentService;

    @LogOperationAop
    @Operation(summary = "分页查询信息")
    @PostMapping("/page/target/{targetId}")
    public ResultModel<PageModel<CommentVo>> page(
            @Parameter(description = "评论目标id", required = true) @PathVariable Long targetId,
            @RequestBody CommentQuery query
    ) {
        query.setTargetId(targetId);
        var pageModel = commentService.page(query);
        return R.success(pageModel);
    }

    @LogOperationAop
    @Operation(summary = "添加回复信息")
    @PostMapping("/")
    public ResultModel<Boolean> add(
            @RequestBody CommentDto dto
    ) {
        dto.check();
        var loginUser = SessionUserUtil.getLoginUser();
        commentService.save(loginUser.userId(), dto.getTargetId(), dto.getCommentId(), dto.getContent());
        return R.success(true);
    }

    @LogOperationAop
    @Operation(summary = "删除信息")
    @DeleteMapping("/{commentId}")
    public ResultModel<Boolean> delete(
            @Parameter(description = "主键id", required = true) @PathVariable Long commentId
    ) {
        var loginUser = SessionUserUtil.getLoginUser();
        commentService.deleteByUserId(loginUser.userId(), commentId);
        return R.success(true);
    }

}
